package fantasyday.wowzolo.web.account;

import fantasyday.wowzolo.domain.Account;
import fantasyday.wowzolo.service.AccountService;
import fantasyday.wowzolo.web.catalog.IndexPage;
import fantasyday.wowzolo.web.common.BasePage;
import fantasyday.wowzolo.web.common.ErrorMessage;
import fantasyday.wowzolo.web.common.SecurePage;
import fantasyday.wowzolo.web.order.ListOrdersPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.spring.injection.annot.SpringBean;

@SecurePage
public class EditAccountPage extends BasePage {
	@SpringBean
	private AccountService accountService;

	public EditAccountPage() {
		String username = getSignonedAccount().getUsername();
		Account account = accountService.getAccount(username);
		
		Form form = new AccountForm("editForm", account) {
			@Override
			protected void onSubmit() {
                try {
                    Account account = (Account) getModelObject();
                    accountService.updateAccount(account);
                    account.setPassword(null);
                    setSignonedAccount(account);
                    setResponsePage(IndexPage.class);
                } catch (Exception e) {
                    error(ErrorMessage.editError + e);
                }
            }
		};		
		
		form.add(new Label("username"));
		
		add(form);
		
		add(new PageLink("listOrders", ListOrdersPage.class));
	}
}
